//监听鼠标滚动事件，目前测试firefox ie

var page = page||{};
(function(page){
	var scrolls = function(event){
		var loading = page.scrolls.defaults.loading;
		if(loading){
			return;
		}
		var target = page.scrolls.defaults.target.document;
		var totalH = target.body.offsetHeight;
		//var clientH= window.document.body.clientHeight;
		var clientH= target.documentElement.clientHeight||target.body.clientHeight;
        var scrollH= target.documentElement.scrollTop||target.body.scrollTop;
        //var scrollH= window.document.documentElement.scrollTop||window.document.body.scrollTop;
        
        if(scrollH>totalH-clientH-80){
        	//if(scrollH>clientH-80){
        	page.scrolls.defaults.loading = true;
        	page.scrolls.defaults.container.appendChild(page.scrolls.defaults.notice);
        	
        	data();
        }
	}
	var data = function(){
		var url = page.scrolls.defaults.url;
		var index = page.scrolls.defaults.index++;
		var size = page.scrolls.defaults.size;
		var callBack = page.scrolls.defaults.callBack;
		//document.body.style.height = document.body.offsetHeight + 500 +"px";
		//page.scrolls.defaults.loading = false;
		//loading
		var myajax = getByAjax();
		myajax.init(null,"get",true);
		myajax.get(url+"index="+index+"&size="+size,function(d){
//			var data = eval("("+d+")");
			var data = toJson(d);
			if(!data[0]||data.length==0){
				var target = page.scrolls.defaults.target;
				target.onscroll = null;
				target = null;
				page.scrolls.defaults.notice.innerHTML = "信息已全部加载！";
				page.scrolls.defaults.loading = false;
				return;
			}
			callBack(data);
			page.scrolls.defaults.loading = false;
			page.scrolls.defaults.container.removeChild(page.scrolls.defaults.notice);
			page.scrolls.defaults.notice.innerHTML = "正在加载...请稍后";
		});
		myajax = null;
	}
	function buildUrl(url,data){
		//var url = param.url;
		if(data !=null){
			for(var i in data){
				url = url + i + "=" + data[i]+"&";
			}
			//url = url.slice(0,url.length-1);
		}
		return url;
	}
	var init = function(param){
		var data = page.scrolls.defaults.data = param.data||null;
		
		var url = buildUrl(param.url+"?",data);
		page.scrolls.defaults.url = url;
		page.scrolls.defaults._url = param.url;
		page.scrolls.defaults.index = 1;
		page.scrolls.defaults.size = param.size||30;
		page.scrolls.defaults.callBack = param.callBack;
		page.scrolls.defaults.target = param.target||window;
		page.scrolls.defaults.container = document.getElementById(param.containerId);
		if(!page.scrolls.defaults.notice){
			var notice = document.createElement("span");
			with(notice.style){
				display = "block";
				width = "100%";
				//maxWidth = "800px";
				height = "20px";
				fontSize = "16px;";
				color = "green";
				backgroundColor = "gray";
				lineHeight = "20px";
				verticalAlign = "middle";
				borderRadius = "3px";
				clear="both";
				textAlign="center"; 
				position = "relative";
				overflow = "hidden";
				margin = "10px 0px";
				left = "0px";
				bottom = "0px";
			}
			notice.innerHTML = "正在加载...请稍后";
			page.scrolls.defaults.notice = notice;
		}
	}
	/*
	 * param={
	 * 		'url':"",
	 * 		'callBack':function,
	 * 		'size':30
	 * }
	 * */
	page.scrolls = function(param){
		init(param);
		var target = page.scrolls.defaults.target;
		//window.onscroll = scrolls;
		target.onscroll = scrolls;
		target = null;
	}
	page.scrolls.init = function(param){
		init(param);
	}
	page.scrolls.setIndex = function(index){
		page.scrolls.defaults.index = 1;
		page.scrolls.defaults.loading = false;
		if(!page.scrolls.defaults.target.onscroll){
			page.scrolls.defaults.target.onscroll = scrolls;
		}
	};
	page.scrolls.method={
		_getUrl:function(){
			return page.scrolls.defaults._url;
		},
		getUrl:function(){
			return page.scrolls.defaults.url;
		},
		setUrl:function(url,data){
			var d = page.scrolls.defaults.data||{};
			for(var i in data){
				d[i] = data[i];
			}
			url = buildUrl(url+"?",d);
			page.scrolls.defaults.url = url;
			
			var target = page.scrolls.defaults.target;
			target.onscroll = scrolls;
			target = null;
		},
		setIndex:function(index){
			page.scrolls.defaults.index = index ||1;
		},
		getData:function(){
			return page.scrolls.defaults.data||{};
		}
	}
	page.scrolls.defaults = {
		loading:false,
		url:"",
		data:{},
		index:1,
		size:30,
		callBack:null,
		target:window,
		container:null,
		notice:null,
		_url:""
	}
})(page);



